package study.transactionStudy.service;

import javax.annotation.Resource;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import study.transactionStudy.dao.CustomerRepository;
import study.transactionStudy.domain.Customer;

/**
 * @author jiangfeng 2019/7/9 12:31
 */
@Slf4j
@Service
public class CustomerServiceTxInAnnotation {

    @Autowired
    CustomerRepository customerRepository;

    @Autowired
    JmsTemplate jmsTemplate;

    @Autowired
    ConnectionFactory connectionFactory;

//    rabbitMq 的用法
//    @Resource
//    @Qualifier(OrderSource.OUTPUT_DAILY_REPORT)
//    private MessageChannel outputDailyReport;

    @Transactional
    public Customer create(Customer customer) {
        Customer save = customerRepository.save(customer);
        if(customer.getName().contains("error")){
            throw new RuntimeException("some error");
        }
        return save;
    }


    @Transactional
    public Customer create2(Customer customer) {
        Customer save = customerRepository.save(customer);
        if(customer.getName().contains("error")){
            throw new RuntimeException("some error");
        }

        return save;
    }

    @Transactional
    public Customer createMq(Customer customer) throws JMSException {
        // 2.创建连接 (连接池中获取)
        Connection connection = connectionFactory.createConnection();
        // 3.启动连接
        connection.start();
        // 4.创建会话
        Session session = connection.createSession();
        // 5.创建一个目标(队列模式)
        Destination queueDestination = session.createQueue("queue-test");
        // 创建主题模式
        // Destination topicDestination = session.createTopic("topic-test");
        // 6.创建生产者(用来发送消息)
        MessageProducer producer = session.createProducer(queueDestination);
        //7.创建消息
        TextMessage textMessage = session.createTextMessage("test" + customer);
        //8.发布消息
        producer.send(textMessage);
        System.out.println("发送消息" + textMessage.getText());
        // 9.关闭连接
        connection.close();
        if (customer.getName().contains("error")) {
            throw new RuntimeException("some error");
        }
        return customer;
    }

   /* 监听收到的信息
   @StreamListener(OrderSink.INPUT_DAILY_REPORT)
    public void input(Message<String> message) {

       // 发送消息
       outputDailyReport.send(MessageBuilder.withPayload(resultMessage).build());


        log.info("收到日报消息msg: {}", message.getPayload());
        businessAlarmService.itilAlarmPush(rcmsLoanCount, "零售进件信息统计", message.getPayload());
    }*/
}
